from pydantic import BaseModel, EmailStr, Field
from datetime import datetime
from typing import Optional, List


class UserRegister(BaseModel):
    """用户注册请求模型"""
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    password: str = Field(..., min_length=5, max_length=50, description="密码")
    confirmPassword: str = Field(..., min_length=5, max_length=50, description="确认密码")
    email: EmailStr = Field(..., description="邮箱")


class UserLogin(BaseModel):
    """用户登录请求模型"""
    username: str = Field(..., description="用户名")
    password: str = Field(..., description="密码")


class UserResponse(BaseModel):
    """用户响应模型"""
    id: int
    username: str
    email: str
    created_at: datetime

    class Config:
        from_attributes = True


class Token(BaseModel):
    """Token响应模型"""
    access_token: str
    token_type: str = "bearer"
    user: UserResponse


class TokenData(BaseModel):
    """Token数据模型"""
    username: Optional[str] = None


class UserUpdate(BaseModel):
    """用户更新请求模型"""
    username: Optional[str] = Field(None, min_length=3, max_length=50, description="用户名")
    email: Optional[EmailStr] = Field(None, description="邮箱")
    password: Optional[str] = Field(None, min_length=5, max_length=50, description="新密码")

    class Config:
        from_attributes = True


class UserListResponse(BaseModel):
    """用户列表响应模型"""
    total: int
    users: list[UserResponse]


# ==================== 配置管理相关模型 ====================

class VideoResource(BaseModel):
    """视频资源模型"""
    id: str = Field(..., description="视频ID")
    name: str = Field(..., description="视频名称")
    url: str = Field(..., description="视频URL")
    keywords: List[str] = Field(default_factory=list, description="关键词列表")


class SystemConfigUpdate(BaseModel):
    """系统配置更新请求模型"""
    avatar_prompt: Optional[str] = Field(None, description="数字人提示词配置")
    internal_knowledge_id: Optional[str] = Field(None, description="内部知识库ID")
    internal_knowledge_api_key: Optional[str] = Field(None, description="内部知识库API密钥")
    external_knowledge_id: Optional[str] = Field(None, description="外部知识库ID")
    external_knowledge_api_key: Optional[str] = Field(None, description="外部知识库API密钥")
    ai_model_api_url: Optional[str] = Field(None, description="AI模型API地址")
    ai_model_name: Optional[str] = Field(None, description="AI模型名称")
    ai_model_api_key: Optional[str] = Field(None, description="AI模型API密钥")
    voice_config: Optional[str] = Field(None, description="音色配置")
    floor_guide_image: Optional[str] = Field(None, description="楼层导览图片路径")
    video_resources: Optional[List[VideoResource]] = Field(None, description="视频资源配置")

    class Config:
        from_attributes = True


class SystemConfigResponse(BaseModel):
    """系统配置响应模型"""
    id: int
    avatar_prompt: Optional[str] = None
    internal_knowledge_id: Optional[str] = None
    internal_knowledge_api_key: Optional[str] = None
    external_knowledge_id: Optional[str] = None
    external_knowledge_api_key: Optional[str] = None
    ai_model_api_url: Optional[str] = None
    ai_model_name: Optional[str] = None
    ai_model_api_key: Optional[str] = None
    voice_config: Optional[str] = None
    floor_guide_image: Optional[str] = None
    video_resources: Optional[List[VideoResource]] = None
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


# ==================== 历史对话管理相关模型 ====================

class Message(BaseModel):
    """消息模型"""
    id: int = Field(..., description="消息ID")
    role: str = Field(..., description="角色(user/assistant)")
    content: str = Field(..., description="消息内容")
    status: str = Field(default="normal", description="消息状态")

    class Config:
        from_attributes = True


class ConversationResponse(BaseModel):
    """对话历史响应模型"""
    id: int = Field(..., description="对话ID")
    session_id: str = Field(..., description="会话ID")
    title: str = Field(..., description="对话标题")
    messages: List[Message] = Field(default_factory=list, description="消息列表")
    source: Optional[str] = Field(default="internal", description="对话来源: internal-内部聊天, external-外部聊天")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="最近更新时间")

    class Config:
        from_attributes = True


class ConversationListResponse(BaseModel):
    """对话历史列表响应模型"""
    total: int = Field(..., description="总记录数")
    conversations: List[ConversationResponse] = Field(..., description="对话列表")

    class Config:
        from_attributes = True


# ==================== 问题管理相关模型 ====================

class QuestionCreate(BaseModel):
    """问题创建请求模型"""
    title: str = Field(..., min_length=4, max_length=500, description="问题标题")
    category: str = Field(..., min_length=1, max_length=100, description="问题分类")

    class Config:
        from_attributes = True


class QuestionUpdate(BaseModel):
    """问题更新请求模型"""
    title: Optional[str] = Field(None, min_length=4, max_length=500, description="问题标题")
    category: Optional[str] = Field(None, min_length=1, max_length=100, description="问题分类")
    status: Optional[str] = Field(None, description="问题状态：active-启用, inactive-停用")

    class Config:
        from_attributes = True


class QuestionResponse(BaseModel):
    """问题响应模型"""
    id: int = Field(..., description="问题ID")
    title: str = Field(..., description="问题标题")
    category: str = Field(..., description="问题分类")
    status: str = Field(..., description="问题状态")
    usage_count: int = Field(default=0, description="使用次数")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="最近更新时间")

    class Config:
        from_attributes = True


class QuestionListResponse(BaseModel):
    """问题列表响应模型"""
    total: int = Field(..., description="总记录数")
    questions: List[QuestionResponse] = Field(..., description="问题列表")

    class Config:
        from_attributes = True

